/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import Utils.Query_Login;
import Utils.User;
import java.io.IOException;
import javax.annotation.Resource;
import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Daniel
 */
@WebServlet(name = "Login", urlPatterns = {"/Login" , "/Logout"})
public class Login extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    /*
     * 
     * author = DANIEL  
     * usata = al login imposta la sessione (serve per controllare chi è connesso e , in futuro , per passare i dati    
     * come username o email o matricola in modo da avere un identificativo per fare le query 
     *
     * 
     * 
     */
    
    
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String contex = request.getRequestURI();
        
        if(contex.equals("/Sito_ingegneria/Login"))
           { 
        HttpSession session = request.getSession();

        String password = request.getParameter("password");
        String user  = request.getParameter("user"); //Nome di login , chiave primaria che verra usata nelle hash
           
        User x = Query_Login.query_login(user, password);
         
        switch(x.getRole())
        {
            case 3 : 
                     session.setAttribute("user", x);
                     response.sendRedirect("Home_Studente");
                     break;
            case 2 : 
                     session.setAttribute("user", x);
                     response.sendRedirect("Corsi_Docente");
                     break;
            case 7 : 
                     session.setAttribute("user", x);
                     response.sendRedirect("Studenti_Segreteria");
                     break;
            default :
                    session.invalidate();
                    response.sendRedirect("index.jsp?error=User name o password errati.");
                    
        }
           }
            else
           {
           HttpSession session = request.getSession();
              
           User user = (User) session.getAttribute("user"); 
           
           Query_Login.delete(user.getName());
           session.invalidate();
           
           response.sendRedirect("index.jsp");
           } 
       
          
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>


}
